cmake_minimum_required(VERSION 3.15)

# add cpr
if (USE_SYSTEM_CPR)
    find_package(cpr REQUIRED GLOBAL)
else ()
    if (USE_SYSTEM_CURL)
        set(CPR_USE_SYSTEM_CURL ON)
    else ()
        set(CPR_USE_SYSTEM_CURL OFF)
    endif ()

    set(BUILD_SHARED_LIBS OFF)
    set(CPR_ENABLE_SSL ON)
    if (PLATFORM_DESKTOP)
        if (WIN32)
            set(CPR_USE_SYSTEM_CURL OFF)
            set(CPR_FORCE_WINSSL_BACKEND ON)
        endif ()
    elseif (PLATFORM_IOS)
        set(CPR_USE_SYSTEM_CURL OFF)
        set(CPR_SKIP_CA_BUNDLE_SEARCH ON)
    elseif (PLATFORM_SWITCH)
        set(CPR_USE_SYSTEM_CURL ON)
        set(CPR_FORCE_MBEDTLS_BACKEND ON)
    endif ()
    add_subdirectory(cpr EXCLUDE_FROM_ALL)
endif ()

# add libopencc
if (NOT USE_LIBROMFS AND NOT DISABLE_OPENCC AND NOT PLATFORM_IOS AND NOT PLATFORM_PSV)
    set(BRLS_USE_OPENCC ON)
    if (USE_SYSTEM_OPENCC)
        find_package(PkgConfig REQUIRED)
        pkg_check_modules(opencc REQUIRED IMPORTED_TARGET opencc)
        add_library(libopencc ALIAS PkgConfig::opencc)
        set(BRLS_OPENCC_INCLUDE ${opencc_INCLUDE_DIRS})
    else ()
        set(BUILD_SHARED_LIBS OFF)
        set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
        add_subdirectory(OpenCC EXCLUDE_FROM_ALL)
        target_compile_options(libopencc PRIVATE -w)
        set(BRLS_OPENCC_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/OpenCC/src)
    endif ()
else ()
    set(BRLS_USE_OPENCC OFF)
endif ()

# add QR-Code-generator
if (USE_SYSTEM_QRCODEGEN)
    find_package(qrcodegencpp REQUIRED GLOBAL)
    add_library(qrcode ALIAS qrcodegencpp::qrcodegencpp)
else ()
    add_library(qrcode STATIC ${CMAKE_CURRENT_SOURCE_DIR}/QR-Code-generator/cpp/qrcodegen.cpp)
    target_include_directories(qrcode PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/QR-Code-generator/cpp)
    set_property(TARGET qrcode PROPERTY CXX_STANDARD 17)
endif ()

# add pystring
if (USE_SYSTEM_PYSTRING)
    find_path(PYSTRING_INCLUDE_DIR pystring.h PATH_SUFFIXES pystring REQUIRED)
    find_library(PYSTRING_LIBRARY NAMES pystring REQUIRED)
    list(APPEND APP_PLATFORM_INCLUDE ${PYSTRING_INCLUDE_DIR})
    list(APPEND APP_PLATFORM_LIB ${PYSTRING_LIBRARY})
    set(APP_PLATFORM_LIB ${APP_PLATFORM_LIB} PARENT_SCOPE)
    set(APP_PLATFORM_INCLUDE ${APP_PLATFORM_INCLUDE} PARENT_SCOPE)
else ()
    add_library(pystring STATIC ${CMAKE_CURRENT_SOURCE_DIR}/pystring/pystring.cpp)
    target_include_directories(pystring PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/pystring)
    set_property(TARGET pystring PROPERTY CXX_STANDARD 17)
endif ()

# add lunasvg
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
add_subdirectory(lunasvg EXCLUDE_FROM_ALL)

# add mongoose
add_subdirectory(mongoose EXCLUDE_FROM_ALL)

# add borealis
add_subdirectory(borealis/library)

# add libpdr (不使用项目内的 cmake 配置，因为 wiliwili 使用支持 switch 的 mongoose)
file(GLOB_RECURSE PDR_SRC ${CMAKE_CURRENT_SOURCE_DIR}/libpdr/src/*.cpp)
add_library(pdr ${PDR_SRC})
set_property(TARGET pdr PROPERTY CXX_STANDARD 17)
target_include_directories(pdr PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/libpdr/include)
target_link_libraries(pdr mongoose tinyxml2)
if (PLATFORM_SWITCH)
    target_compile_options(pdr PUBLIC -D__SWITCH__ -DMG_ARCH=10086)
elseif (PLATFORM_PSV)
    target_compile_options(pdr PUBLIC -D__PSV__ -DMG_ARCH=10010)
endif ()